import numpy as np
import matplotlib.pyplot as plt

# 定义次品率作为伯努利分布的参数
situations = {
    "LPJ1": [0.10, 0.20, 0.10, 0.20, 0.10, 0.05],
    "LPJ2": [0.10, 0.20, 0.10, 0.20, 0.20, 0.05],
    "CP": [0.10, 0.20, 0.10, 0.20, 0.20, 0.05]
}

# 模拟每种情况的次品率
num_samples = 100000  # 样本数量
results = {key: [] for key in situations}

# 生成样本
for item, rates in situations.items():
    for rate in rates:
        samples = np.random.binomial(1, rate, num_samples)
        results[item].append(np.mean(samples))

# 可视化
labels = list(situations.keys())
x = np.arange(len(situations["LPJ1"]))  # 情境数量

# 创建柱状图
width = 0.25  # 柱状图宽度
fig, ax = plt.subplots()

# 绘制每种产品类别
for i, item in enumerate(results.keys()):
    ax.bar(x + i * width, results[item], width, label=item)

# 添加标签
ax.set_xlabel('QK')
ax.set_ylabel('Mean sample defect rate')
ax.set_title('Defect rate of each situation')
ax.set_xticks(x + width)
ax.set_xticklabels([f'QK {i+1}' for i in range(len(situations["LPJ1"]))])
ax.legend()

# 显示图形
plt.tight_layout()
plt.show()
